package com.iaz.tech.tools.common.utils;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.util.HashMap;
import java.util.Map;

@Data
public class AZChain<T> implements IChainNodeAble<T> {
    /**
     * 日志
     */
    private final static Logger logger = (Logger) LoggerFactory.getLogger( AZChain.class );
    private static long __iNNER_SCHEDULE_SERAILNO = 0L;
    private final static String __SYN_SCHEDULE_SERAILNO = "_N_LOCK_FOR_CHAIN_SERIAL";
    private long chainID;
    
    T data;

    IChainNodeAble<T> next;
    IChainNodeAble<T> prev;

    /* 子链表 */
    IChainNodeAble<T> down;
    IChainNodeAble<T> upper;
    
    public AZChain() {
    	this.data = null;
        this.prev = null;
        this.next = null;
        this.chainID = AZChain.GenerateScheduleCode();
    }

    public AZChain(T data) {
        this.data = data;
        this.prev = null;
        this.next = null;
        this.chainID = AZChain.GenerateScheduleCode();
    }

    /**
     * 获得任务的ID号，在系统重是唯一的
     * @return 当前时间戳并合并000
     */
    public static long GenerateScheduleCode() {
        long result = -1L;
        synchronized( __SYN_SCHEDULE_SERAILNO ) {
            if( __iNNER_SCHEDULE_SERAILNO >= 9999) {
                __iNNER_SCHEDULE_SERAILNO= 0L;
            }
            __iNNER_SCHEDULE_SERAILNO++;
            result = UtilTools.getCurrentTime() * 10000 + __iNNER_SCHEDULE_SERAILNO;
        }
        return result;
    }

    
    public boolean isEmpty() {
        return false;  
    }
    
    @Override
    public IChainNodeAble<T> nextChain() {
        return this.next;
    }
    
    @Override
	public void putNext(IChainNodeAble<T> _chainNode) {
		this.next = _chainNode;
	}
    
    @Override
    public IChainNodeAble<T> prevChain() {
        return this.prev;
    }
    
	@Override
	public void putPrev(IChainNodeAble<T> _chainNode) {
		this.prev = _chainNode;		
	}
	
	@Override
	public IChainNodeAble<T> downChain() {
		return this.down;
	}
	
	@Override
	public void putDown(IChainNodeAble<T> _chainNode) {
		this.down = _chainNode;		
	}
	@Override
	public IChainNodeAble<T> upperChain() {
		return this.upper;
	}
	@Override
	public void putUpper(IChainNodeAble<T> _chainNode) {		
		this.upper = _chainNode;
	}
    
    @Override
	public long chainIdentifyNo() {
		return this.chainID;
	}

   
    @Override
    public T toData() {
        return this.data;
    }    
    /**
     * 填充数据
     * @param data
     */
	@Override
    public void pushData(T _data) {
		this.data = _data;
	}

	@Override
	public boolean isHeader() {
		return (this.prev == null);
	}

	@Override
	public boolean isTail() {
		return (this.next == null);
	}

	@Override
	public boolean hasChildren() { 
		return (this.down != null );
	}

	@Override
	public boolean hasUpper() { 
		return (this.upper != null);
	}

	@Override
	public boolean containerChildren() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void clear() {
	}

	@Override
	public IChainNodeAble<T> reset() {
		return this;
	}
}
